package com.sgb.kjwl.viewmodel.enterprise;

import android.widget.Toast;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.enterprise.CompanyListEntity2;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.model.entity.enterprise.PropertyEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.fragment.enterprise.MakeOverFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MakeOverModel extends BaseViewModel {

    MakeOverFragment makeOverFragment;
    IpPickSelectEntity ipPickSelectEntity = new IpPickSelectEntity("", "不限", "", "", "", "", "");

    public MakeOverModel(MakeOverFragment makeOverFragment) {
        this.makeOverFragment = makeOverFragment;
    }

    public void getCompanyList(Map<String, Object> map) {
        if (((int)map.get("page")) == 1){
            makeOverFragment.loading();
        }
        DialogHelper.showProgressDialog(makeOverFragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().companyList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CompanyListEntity2>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CompanyListEntity2>>>> call, Response<BaseEntity<ListResponse<List<CompanyListEntity2>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    makeOverFragment.getCompanyList(response.body().getData().getList());
                    makeOverFragment.success();
                } else {
                    Toast.makeText(makeOverFragment.getContext(), response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    makeOverFragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CompanyListEntity2>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                makeOverFragment.error();
            }
        });
    }

    /**
     * 商标
     */
    public void getTrademarkTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTrademarkTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showBrandPopWindow(list);
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 价格
     */
    public void getPriceChangeOption() {
        NewAPIManager.getInstance().getEnterPrisenewAPI().getPropertyPriceOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showPricePopWindow(list, true);
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getPriceOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCompanyPriceOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showPricePopWindow(list, true);
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取组合类型
     */
    public void getTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showMakeupPopWindow(response.body().getData());
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), "暂无数据");
                }
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取长度选项
     */
    public void getStrLengthOption() {

        NewAPIManager.getInstance().getEnterPrisenewAPI().getStrLengthOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showLengthPopWindow(response.body().getData());
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), "暂无数据");
                }
                DialogHelper.dismissProgressDialog();
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 获取版权分类选项
     */
    public void getCopyrightTypeOption() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getCopyrightTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(makeOverFragment.getActivity(), "暂无数据");
                    return;
                }
                if (response.body().getMsg().equals("执行成功")) {
                    makeOverFragment.showBrandPopWindow(response.body().getData());
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), response.body().getMsg());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*
     * 获取是否独家选项 本地写入数据
     */
    public void getIsOnlyOption() {
        IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");
        IpPickSelectEntity entity2 = new IpPickSelectEntity("1", "独家授权", "", "", "", "", "");
        IpPickSelectEntity entity3 = new IpPickSelectEntity("2", "非独家授权", "", "", "", "", "");
        DialogHelper.dismissProgressDialog();
        List<IpPickSelectEntity> onlyDatas = new ArrayList<>();
        onlyDatas.add(entity1);
        onlyDatas.add(entity2);
        onlyDatas.add(entity3);
        makeOverFragment.showMakeupPopWindow(onlyDatas);
    }

    /**
     * 获取行业分类
     */
    public void queryIndustryCategry() {
        DialogHelper.showProgressDialog(makeOverFragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getTradeTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showBrandPopWindow(list);
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 专利类型
     */
    public void queryPatentTypeOption() {
        DialogHelper.showProgressDialog(makeOverFragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPatentTypeOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showBrandPopWindow(response.body().getData());
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 企业类型
     */
    public void getCompanyType() {
        IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");
        IpPickSelectEntity entity2 = new IpPickSelectEntity("0", "有限责任公司", "", "", "", "", "");
        IpPickSelectEntity entity3 = new IpPickSelectEntity("1", "个人独资公司", "", "", "", "", "");
        IpPickSelectEntity entity4 = new IpPickSelectEntity("2", "港澳台公司", "", "", "", "", "");
        IpPickSelectEntity entity5 = new IpPickSelectEntity("3", "集团公司", "", "", "", "", "");
        List<IpPickSelectEntity> onlyDatas = new ArrayList<>();
        onlyDatas.add(entity1);
        onlyDatas.add(entity2);
        onlyDatas.add(entity3);
        onlyDatas.add(entity4);
        onlyDatas.add(entity5);
        makeOverFragment.showPricePopWindow(onlyDatas, false);
    }

    /**
     * 纳税类型
     */
    public void getRatePayingType() {
        IpPickSelectEntity entity1 = new IpPickSelectEntity("", "不限", "", "", "", "", "");
        IpPickSelectEntity entity2 = new IpPickSelectEntity("0", "小规模", "", "", "", "", "");
        IpPickSelectEntity entity3 = new IpPickSelectEntity("1", "一般纳税人", "", "", "", "", "");
        IpPickSelectEntity entity4 = new IpPickSelectEntity("2", "出口纳税", "", "", "", "", "");
        IpPickSelectEntity entity5 = new IpPickSelectEntity("3", "未核税", "", "", "", "", "");
        List<IpPickSelectEntity> onlyDatas = new ArrayList<>();
        onlyDatas.add(entity1);
        onlyDatas.add(entity2);
        onlyDatas.add(entity3);
        onlyDatas.add(entity4);
        onlyDatas.add(entity5);
        makeOverFragment.showMakeupPopWindow(onlyDatas);
    }

    /**
     * 资产分类
     */
    public void queryPropertyType() {
        DialogHelper.showProgressDialog(makeOverFragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().queryPropertyType().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showBrandPopWindow(list);
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getPropertyList(Map<String, Object> map) {
        if ((int)map.get("page") == 1){
            makeOverFragment.loading();
        }
        DialogHelper.showProgressDialog(makeOverFragment.getActivity(), null, "数据获取中...", 0, false, null).setCanceledOnTouchOutside(false);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().propertyList(map).enqueue(new Callback<BaseEntity<ListResponse<List<PropertyEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<PropertyEntity>>>> call, Response<BaseEntity<ListResponse<List<PropertyEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null)
                    return;
                if (response.body().getState().equals("ok")) {
                    makeOverFragment.getPropertyList(response.body().getData().getList());
                    makeOverFragment.success();
                } else {
                    Toast.makeText(makeOverFragment.getContext(), response.body().getMsg(), Toast.LENGTH_SHORT).show();
                    makeOverFragment.error();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<PropertyEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
                makeOverFragment.error();
            }
        });
    }

    public void getNewOrOldType() {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPropertyNewOrOldOption().enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                List<IpPickSelectEntity> list = new ArrayList<>();
                list.addAll(response.body().getData());
                list.add(0, ipPickSelectEntity);
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    makeOverFragment.showLengthPopWindow(list);
                } else {
                    MToast.showToast(makeOverFragment.getActivity(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /*
     * 获取发布类型
     */
    public void getPublishTypeOption() {
        List<IpPickSelectEntity> onlyDatas = new ArrayList<>();
        IpPickSelectEntity entity = new IpPickSelectEntity();
        entity.setName("不限");
        entity.setPublishType("");
        onlyDatas.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("团队");
        entity.setPublishType("2");
        onlyDatas.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("个人");
        entity.setPublishType("0");
        onlyDatas.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("企业");
        entity.setPublishType("1");
        onlyDatas.add(entity);

        makeOverFragment.showMakeupPopWindow(onlyDatas);

    }

    public void getPriceOptionByNo(int priceServiceNo) {
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPriceOption(priceServiceNo).enqueue(new Callback<BaseEntity<List<IpPickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<IpPickSelectEntity>>> call, Response<BaseEntity<List<IpPickSelectEntity>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    ArrayList<IpPickSelectEntity> list1 = new ArrayList<>();
                    list1.addAll(response.body().getData());
                    list1.add(0, ipPickSelectEntity);
                    switch (priceServiceNo) {
                        case 3://资产
                            makeOverFragment.showPricePopWindow(list1, true);
                            break;
                        case 2://公司
                            makeOverFragment.showPricePopWindow1(list1);
                            break;
                    }

                } else {
                    MToast.showToast(makeOverFragment.getContext(), "暂无数据");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<IpPickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
